﻿using System.Text;
using System.Net;
using System.IO;
using System.Xml;

namespace BloggingServicePinger
{
    class HttpPingerClass
    {
        private HttpStatusCode httpStatusCode;
        private string pingResponse;
        
        public HttpStatusCode HttpStatusCode
        {
            get { return httpStatusCode; }
        }
        public string HttpStatusDescription { get; set; }
        public string PingResponse
        {
            get { return pingResponse; }
        }

        public HttpPingerClass(string userAgent, string bloggingServerUri, string myBlogUri, string myBlogName)
        {
            HttpWebRequest wreq = (HttpWebRequest)HttpWebRequest.Create(bloggingServerUri);
            wreq.AllowAutoRedirect = false;
            wreq.UserAgent = userAgent;
            wreq.ContentType = "text/xml";
            wreq.Timeout = 20000;
            wreq.Method = "POST";
            wreq.KeepAlive = false;
            Stream stream = wreq.GetRequestStream();
            XmlTextWriter xtw = new XmlTextWriter(stream, Encoding.ASCII);
            xtw.WriteStartDocument();
            xtw.WriteStartElement("methodCall");
            xtw.WriteElementString("methodName", "weblogUpdates.ping");
            xtw.WriteStartElement("params");
            xtw.WriteStartElement("param");
            xtw.WriteElementString("value", myBlogName);
            xtw.WriteEndElement();
            xtw.WriteStartElement("param");
            xtw.WriteElementString("value", myBlogUri);
            xtw.WriteEndElement();
            xtw.WriteEndElement();
            xtw.WriteEndElement();
            xtw.Flush();
            xtw.Close();
            stream.Close();

            try
            {
                HttpWebResponse wres = (HttpWebResponse)wreq.GetResponse();
                httpStatusCode = wres.StatusCode;
                HttpStatusDescription = wres.StatusDescription;
                //XPathDocument doc = new XPathDocument(wres.GetResponseStream());
                //XPathNavigator nav = doc.CreateNavigator();
                //bool flerror = bool.Parse(nav.SelectSingleNode("//methodResponse/params/param/value/struct/member[name=\"flerror\"]/value/boolean").Value);
                //string message = nav.SelectSingleNode("//methodResponse/params/param/value/struct/member[name=\"message\"]/value/string").Value;
                StreamReader sr = new StreamReader(wres.GetResponseStream());
                pingResponse = sr.ReadToEnd();
                string debugout = string.Format("Response with {0} - details: {1}", bloggingServerUri, pingResponse);
                System.Diagnostics.Trace.WriteLine(debugout);
            }
            catch (WebException webEx)
            {
                if (webEx.Response != null)
                {
                    httpStatusCode = ((HttpWebResponse)webEx.Response).StatusCode;
                    HttpStatusDescription = ((HttpWebResponse)webEx.Response).StatusDescription;
                }
                else
                {
                    httpStatusCode = HttpStatusCode.NotFound;
                    HttpStatusDescription = webEx.Message;
                }
                pingResponse = webEx.Message;
                
                string debugout = string.Format("Exception with {0} - details: {1}", bloggingServerUri, webEx.Message);
                System.Diagnostics.Trace.WriteLine(debugout);
            }
            finally
            {
                stream.Dispose();
            }

        }


    }
}
